#!/bin/bash
#/bin/bash /usr/local/data/init.sh bxn500 BX998 6.2.0 office,score,timetable
if [ ! -n "${1}" ] || [ ! -n "${2}" ]; then
        echo "ERROR! param error, request rootPassword,schoolCode,your param is : $*"
        exit -1;
fi
rootPassword=${1}
schoolCode=${2}

testMysql=`mysql -uroot -p${1} -e "show databases;"`
result=`echo $testMysql|grep mysql`
if [ -z "$result"  ];then
    echo "ERROR! mysql start failure. data:$testMysql"
    exit -1;
fi

log="/var/log/mysql/mylog/install.log"
if [ ! -f "$log" ]; then
        mkdir -p /var/log/mysql/mylog
        touch $log
fi
echo "$(date +%Y%m%d%k%M%S) init base data begin." >> $log
dataPath=/usr/local/data
#1. cp sql script
if [ ! -d "$dataPath" ]; then
        echo "ERROR! $dataPath is not exist."
        exit -1;
fi
rm -rf /usr/local/install
cp -r /usr/local/data/oe /usr/local/install
sed -i "s/{schoolcode}/$schoolCode/g" /usr/local/install/*.sql

mysql -uroot -p"$rootPassword" < /usr/local/install/onlineelective.sql &> /var/log/mysql/mylog/init-oe.log
mysql -uroot -p"$rootPassword" < /usr/local/install/onlineelective_bxn.sql &> /var/log/mysql/mylog/init-oe-bxn.log

errorlog=`cat /var/log/mysql/mylog/init-oe*.log|grep 'ERROR'`
if [ ! -z "$errorlog" ]; then
        echo $errorlog
        exit -1;
fi
echo "SUCCESS"
